package com.newbye.uis
{
	import com.newbye.component.ComponentFactory;
	import com.newbye.component.ComponentInstanceItem;
	import com.newbye.files.IPanelFile;
	import com.newbye.operate.IGraphicable;
	import com.newbye.operate.SelectBorder;
	import com.newbye.operate.SelectItemMethods;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import mx.containers.Canvas;
	import mx.core.INavigatorContent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	public class EditArea extends Canvas implements INavigatorContent
	{
		private var _parentContainer:DisplayObjectContainer;
		
		private var _parentType:String;
		
		private var _config:*;
		
		private var _uiContaner:UIComponent;
		
		private var _components:Vector.<ComponentInstanceItem> = new Vector.<ComponentInstanceItem>();
		
		private var _panelFile:IPanelFile;
		public function EditArea()
		{
			super();
			this.label = "New UI";
			
			_uiContaner = new UIComponent();
			super.addChild(_uiContaner);
			_uiContaner.graphics.beginFill(0xffcc00, 0.2);
			_uiContaner.graphics.drawRect(0, 0, 1500, 1500);
			_uiContaner.graphics.endFill();
			_uiContaner.width = this.width;
			_uiContaner.height = this.height;
			
			
			addEvents();
		}
	
		public function get panelFile():IPanelFile
		{
			return _panelFile;
		}

		public function set panelFile(value:IPanelFile):void
		{
			_panelFile = value;
			this.label = this._panelFile.className;
		}

		override public function addChild(child:DisplayObject):DisplayObject
		{
			_uiContaner.addChild(child);
			return child;
		}
		
		
		public function get parentType():String
		{
			return _parentType;
		}
		/**
		 *  
		 * @param value 
		 * @see ParentConainerType
		 */		
		public function set parentType(value:String):void
		{
			_parentType = value;
			this._parentContainer = ParentContaierFactory.createParentContainer(this._parentType);
		}

		public function initFromConf(config:*):void
		{
			_config = config;
			initDisplayList();
		}
		
		public function addComponentInstanceItem(comInsItem:ComponentInstanceItem):void
		{
			if(null != comInsItem)
			{
				_components.push(comInsItem);
				addShowItem(comInsItem);
				addComItemEvent(comInsItem);
			}
			
			
//			var tf:TextField = new TextField();
//			tf.text = "sdfsdf";
//			addShowItem(tf);
//			tf.selectable = false;
//			addComItemEvent(tf);
//			
//			var tf2:TextField = new TextField();
//			tf2.text = "aaaaaaaaaaa";
//			tf2.selectable = false;
//			tf2.x = 200;
//			addShowItem(tf2);
//			addComItemEvent(tf2);
			
		}
		
		public function save():void
		{
			
		}
		
		public function dispose():void
		{
			
		}
		
//==========================private============================
		
		private function initDisplayList():void
		{
			if(null == this._parentContainer)
			{
				throw new Error("请先设置parentType");
			}
			this.addChild(this._parentContainer);
			if(!this._config)
			{
				
			}
		}
		
		private function addShowItem(dis:UIComponent):void
		{
			this._uiContaner.addChild(dis);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			_uiContaner.width = this.width;
			_uiContaner.height = this.height;
		}
//=============================events=============================
		private function addEvents():void
		{
			this._uiContaner.addEventListener(MouseEvent.CLICK, onParentClick);
			this._uiContaner.addEventListener(DragEvent.DRAG_DROP, onDragDrop);
			this.addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
		}
		
		private function onDragEnter(e:DragEvent):void
		{
			DragManager.acceptDragDrop(this._uiContaner);
		}
		
		private function onDragDrop(e:DragEvent):void
		{
			var data:* = e.dragSource.dataForFormat("data");
			var className:String = data.@className;
			if(className.length > 3)
			{
				var cii:ComponentInstanceItem = ComponentFactory.createComponentInstanceItem(className);
				cii.addAttr("x", e.localX);
				cii.addAttr("y", e.localY);
				this.addComponentInstanceItem(cii);
			}
		}
		private function onParentClick(e:MouseEvent):void
		{
			if(e.currentTarget == e.target)
			{
				SelectBorder.clearAllBorders();
			}
		}
		
		private function addComItemEvent(dis:UIComponent):void
		{
			dis.addEventListener(MouseEvent.MOUSE_DOWN, comItemMouseDown);
			dis.addEventListener(MouseEvent.MOUSE_UP, comItemMouseUp);
			dis.addEventListener(MouseEvent.MOUSE_OUT, comItemMouseOut);
		}
		
		private function comItemMouseDown(e:MouseEvent):void
		{
			if(e.target == e.currentTarget)
			{
				var dis:UIComponent = e.target as UIComponent;
				if(dis)
				{
					SelectBorder.clearAllBorders();
					SelectItemMethods.selectComponet(dis);
					dis.startDrag();
				}
			}
		}
		
		private function comItemMouseUp(e:MouseEvent):void
		{
			var dis:UIComponent = e.target as UIComponent;
			if(dis)
			{
				//SelectItemMethods.unselectComponet(dis);
				dis.stopDrag();
			}
		}
		
		private function comItemMouseOut(e:MouseEvent):void
		{
			var dis:UIComponent = e.target as UIComponent;
			if(dis)
			{
				dis.stopDrag();
			}
		}
	}
}